Attribute VB_Name = "modNormalizeArray"
'***************************************************************
' (c) Copyright 2000 Matthew J. Curland
'
' This file is from the CD-ROM accompanying the book:
' Advanced Visual Basic 6: Power Techniques for Everyday Programs
'   Author: Matthew Curland
'   Published by: Addison-Wesley, July 2000
'   ISBN: 0-201-70712-8
'   http://www.PowerVB.com
'
' You are entitled to license free distribution of any application
'   that uses this file if you own a copy of the book, or if you
'   have obtained the file from a source approved by the author. You
'   may redistribute this file only with express written permission
'   of the author.
'
' This file depends on:
'   References:
'     VBoostTypes6.olb (VBoost Object Types (6.0))
'     VBoost6.Dll (VBoost Object Implementation (6.0)) (optional)
'   Files:
'     VBoost.Bas (optional)
'   Minimal VBoost conditionals:
'     VBOOST_INTERNAL = 1 : VBOOST_CUSTOM = 1
'   Conditional Compilation Values:
'     NOVBOOST = 1 'Removes VBoost dependency
'
' This file is discussed in Chapter 2.
'***************************************************************
Option Explicit

Private m_SADesc As SafeArray1d
Private m_pSADesc() As SafeArray1d
Private m_SABounds As SafeArray1d
Private m_pSABounds() As SafeArrayBound

Private Sub InitHelperArrays()
    With m_SADesc
        .cDims = 1
        .fFeatures = FADF_AUTO Or FADF_FIXEDSIZE
        .cElements = 1
        .cLocks = 1
        #If NOVBOOST Then
            CopyMemory ByVal VarPtrArray(m_pSADesc), VarPtr(m_SADesc), 4
        #Else
            VBoost.Assign ByVal VarPtrArray(m_pSADesc), VarPtr(m_SADesc)
        #End If
    End With
    With m_SABounds
        .cDims = 1
        .fFeatures = FADF_AUTO Or FADF_FIXEDSIZE
        .cElements = 1
        .cLocks = 1
        .cbElements = LenB(m_pSABounds(0))
        #If NOVBOOST Then
            CopyMemory ByVal VarPtrArray(m_pSABounds), VarPtr(m_SABounds), 4
        #Else
            VBoost.Assign ByVal VarPtrArray(m_pSABounds), VarPtr(m_SABounds)
        #End If
    End With
End Sub
'Returns true if any changes were made
Public Function NormalizeArray(ByVal ppSA As Long, Bound0Start As SafeArrayBound, cDimsStart As Integer) As Boolean
Dim i As Long
Dim lSize As Long
    With m_SADesc
        If .cDims = 0 Then InitHelperArrays
        #If NOVBOOST Then
            CopyMemory .pvData, ByVal ppSA, 4
        #Else
            .pvData = VBoost.Deref(ppSA)
        #End If
        If .pvData = 0 Then Exit Function
        With m_pSADesc(0)
            m_SADesc.pvData = 0
            If .cDims = 1 And .lLbound = 0 Then Exit Function
            cDimsStart = .cDims
            Bound0Start.cElements = .cElements
            Bound0Start.lLbound = .lLbound
            m_SABounds.pvData = VarPtr(.cElements)
            m_SABounds.cElements = .cDims
            lSize = 1
            For i = 0 To .cDims - 1
                lSize = lSize * m_pSABounds(i).cElements
            Next i
            .cDims = 1
            .lLbound = 0
            .cElements = lSize
            m_SABounds.pvData = 0
        End With
    End With
End Function
Public Sub UnNormalizeArray(ByVal ppSA As Long, Bound0Start As SafeArrayBound, ByVal cDimsStart As Integer)
Dim i As Long
Dim lSize As Long
    With m_SADesc
        Debug.Assert .cDims 'Should call NormalizeArray first
        #If NOVBOOST Then
            CopyMemory .pvData, ByVal ppSA, 4
        #Else
            .pvData = VBoost.Deref(ppSA)
        #End If
        If .pvData = 0 Then Exit Sub
        With m_pSADesc(0)
            .cDims = cDimsStart
            .cElements = Bound0Start.cElements
            .lLbound = Bound0Start.lLbound
        End With
        .pvData = 0
    End With
End Sub

Public Function CountArrayElements(ByVal ppSA As Long) As Long
Dim i As Long
Dim lSize As Long
    With m_SADesc
        If .cDims = 0 Then InitHelperArrays
        #If NOVBOOST Then
            CopyMemory .pvData, ByVal ppSA, 4
        #Else
            .pvData = VBoost.Deref(ppSA)
        #End If
        If .pvData = 0 Then Exit Function
        With m_pSADesc(0)
            m_SADesc.pvData = 0
            m_SABounds.pvData = VarPtr(.cElements)
            m_SABounds.cElements = .cDims
            lSize = 1
            For i = 0 To .cDims - 1
                lSize = lSize * m_pSABounds(i).cElements
            Next i
            m_SABounds.pvData = 0
        End With
    End With
    CountArrayElements = lSize
End Function
